﻿(function () {
    $(function () {
        //locahost:2222/app/service   类似先这样的[基于约定]
        var _personService = abp.services.app.person;

        var _$modal = $("#PersonCreateModal");
        var _$form = _$modal.find('form');

        //添加联系人功能
        _$form.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }
            var personEditDto = _$form.serializeFormToObject();//序列化表单对象

            abp.ui.setBusy(_$modal);
            //约定大于配置 
            _personService.createOrUpdatePerson({ personEditDto }).done(function () {
                _$modal.modal('hide');
                refreshPersonList();
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });
        //end 添加结束

        //获取编辑联系人信息
        $('.edit-person').click(function(e) {
            e.preventDefault();
            var personId = $(this).attr("data-person-id");
            _personService.getPersonForPersonEdit({id:personId}).done(function(data) {
                $("input[name=Id]").val(data.person.id);
                $("input[name=Name]").val(data.person.name).parent().addClass('focused');
                $("input[name=EmailAddress]").val(data.person.emailAddress).parent().addClass('focused');
                $("input[name=Address]").val(data.person.address).parent().addClass('focused');
            });
        });
        $("#PersonCreateModal").on("hide.bs.modal",
            function() {
                _$form[0].reset();
            });

        //删除联系人
        $('.delete-person').click(function() {
            var personId = $(this).attr("data-person-id");
            var personName = $(this).attr('data-person-name');
            deletePerson(personId,personName);
        });

        function deletePerson(id, name) {
            abp.message.confirm('是否确认删除姓名为' + name + '的联系人',
                function (isConfirmed) {
                    if (isConfirmed) {
                        _personService.deletePerson({ id }).done(function () {
                            refreshPersonList();
                        });
                    }
                }
            );
        }
        //刷新
        $("#RefreshButton").click(function() {
            refreshPersonList();
        });


        function refreshPersonList () {
            location.reload();
        }


    });

})();